package net.ruangtedy.android.latih.moviesearchapp;

import java.util.EmptyStackException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MovieSearchAppActivity extends Activity {
    private static final String EMPTY_STRING = "";

	private EditText mSearchEditText;
	private RadioButton mMoviesSearchRadioButton;
	private RadioButton mPeopleSearchRadioButton;
	private RadioGroup mSearchRadioGroup;
	private TextView mSearchTypeTextView;
	private Button mSearchButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		findAllViewsById();
		
		mMoviesSearchRadioButton.setOnClickListener(radioButtonListener);
		mPeopleSearchRadioButton.setOnClickListener(radioButtonListener);
		
		mSearchButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String query=mSearchEditText.getText().toString();
				if(mMoviesSearchRadioButton.isChecked()){
					longToast(mMoviesSearchRadioButton.getText()+" "+query);
				}
				else if(mPeopleSearchRadioButton.isChecked()){
					longToast(mPeopleSearchRadioButton.getText()+" "+query);
				}
			
			}
		});
		
		mSearchEditText.setOnFocusChangeListener(new DftTextOnFocusListener(getString(R.string.search)));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void findAllViewsById(){
		mSearchEditText = (EditText) findViewById(R.id.search_edit_text);
		mMoviesSearchRadioButton=(RadioButton) findViewById(R.id.movie_search_radio_button);
		mPeopleSearchRadioButton=(RadioButton) findViewById(R.id.people_search_radio_button);
		mSearchRadioGroup=(RadioGroup) findViewById(R.id.radioGroup1);
		mSearchTypeTextView=(TextView) findViewById(R.id.search_type_text_view);
		mSearchButton=(Button) findViewById(R.id.search_button);
		
	}
	
	private OnClickListener radioButtonListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			RadioButton radioButton=(RadioButton) v;
			mSearchTypeTextView.setText(radioButton.getText());
		}
	};
	
	public void longToast(CharSequence message){
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
	

	private class DftTextOnFocusListener implements OnFocusChangeListener{
		private String defaultText;

		public DftTextOnFocusListener(String defaultText) {
			// TODO Auto-generated constructor stub
			this.defaultText=defaultText;
		}

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if (v instanceof EditText){
				EditText focusedEditText=(EditText) v;
				if(hasFocus) {
					if(focusedEditText.getText().toString().equals(defaultText)){
						focusedEditText.setText(EMPTY_STRING);
					}
				}
			}
		
			
		}
		
	}

}
